<template>
	<view>
		<view class="list" v-for="(item,index) in list">
			<view class="name">{{item.name}}</view>
			<view class="edit">
				<image :src="common.appUrl.oss+common.imgList.sc33" mode="widthFix" @click="top(item.id)"></image>
				<image :src="common.appUrl.oss+common.imgList.sc31" mode="widthFix" @click="edit(item)"></image>
				<image :src="common.appUrl.oss+common.imgList.sc32" mode="widthFix" @click="del(item.id)"></image>
			</view>
		</view>
		<view class="add" @click="show">+添加分类</view>
	</view>
</template>

<script>
	import {
		mapState,
	} from 'vuex';
	export default {
		computed: {
			...mapState(['common'])
		},
		data() {
			return {
				app: getApp().globalData,
				list: []
			}
		},
		onShow() {
			this.getShopClassNameList()
		},
		methods: {
			show() {
				uni.showModal({
					title: '添加分类',
					editable: true,
					placeholderText: '请输入分类名称',
					confirmText: '添加',
					confirmColor: '#84BD00',
					success: (res) => {
						if (res.confirm) {
							this.addShopClassName(res.content)
						}
					}
				});
			},
			top(id) {
				this.app.post('/api/shop.shop/sortShopGoodsCategory', {
					id: id,
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: '操作成功',
						icon: "none"
					})
					this.getShopClassNameList()
				})
			},
			edit(item) {
				uni.showModal({
					title: '修改分类',
					editable: true,
					placeholderText: item.name,
					confirmText: '修改',
					confirmColor: '#84BD00',
					success: (res) => {
						if (res.confirm) {
							this.editShopClassName(res.content, item.id)
						}
					}
				});
			},
			del(id) {
				this.app.post('/api/shop.shop/DelShopCategory', {
					ids: id,
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: '删除成功',
						icon: "none"
					})
					this.getShopClassNameList()
				})
			},
			addShopClassName(name) {
				this.app.post('/api/shop.shop/AddShopCategory', {
					name,
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: '添加成功',
						icon: "none"
					})
					this.getShopClassNameList()
				})
			},
			editShopClassName(name, ids) {
				this.app.post('/api/shop.shop/EditShopCategory', {
					name,
					ids
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: '添加成功',
						icon: "none"
					})
					this.getShopClassNameList()
				})
			},
			getShopClassNameList() {
				this.app.post('/api/shop.shop/GetShopGoodsCategory').then(res => {
					console.log(res)
					this.list = res
				})
			}
		}
	}
</script>

<style lang="scss">
	.list {
		width: 690rpx;
		height: 120rpx;
		margin-left: 30rpx;
		margin-top: 30rpx;
		background-color: #fff;
		border-radius: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.name {
			font-size: 30rpx;
			color: #333333;
			margin-left: 30rpx;
		}

		.edit {
			display: flex;
			margin-right: 30rpx;

			image {
				width: 40rpx;
				height: 40rpx;
				margin-left: 20rpx;
			}
		}
	}

	.add {
		width: 690rpx;
		height: 80rpx;
		border-radius: 10rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		background: rgba(140, 47, 82, 0.1);
		position: fixed;
		bottom: 30rpx;
		left: 30rpx;
		font-size: 30rpx;
		color: #84BD00;
	}
</style>